package com.chaos.framework.model.dto.result.data;

import com.chaos.framework.model.dto.result.Result;
import com.chaos.framework.model.enums.ResultEnum;
import lombok.*;
import lombok.experimental.Accessors;

/**
 * @author Jacky
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class DataResult<T> extends Result {


    private T data;

    public DataResult(ResultEnum re, T data) {
        this.setCode(re.getCode());
        this.setMsg(re.getDefaultMsg());
        this.setData(data);
    }

    public DataResult(Integer code, String msg, T data) {
        this.setCode(code);
        this.setMsg(msg);
        this.setData(data);
    }

    public static <T> DataResult<T> ok(T data) {
        return new DataResult(ResultEnum.OK.getCode(), "success", data);
    }


    public static <T> DataResult<T> error(ResultEnum resultEnum) {
        return new DataResult(resultEnum.getCode(), resultEnum.getDefaultMsg(), null);
    }

    public static <T> DataResult<T> error(Integer errorCode, String msg) {
        return new DataResult(errorCode, msg, null);
    }

    public static <T> DataResult<T> error(Integer errorCode, String msg, T data) {
        return new DataResult(errorCode, msg, data);
    }


}
